package com.dl.mood.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.dl.mood.R;
import com.dl.mood.adapter.MoodListAdapter;
import com.dl.mood.db.MoodDatabase;
import com.dl.mood.model.MoodItem;

import java.util.ArrayList;
import java.util.List;

public class MoodViewModel extends AndroidViewModel {
    private MutableLiveData<List<MoodItem>> moodItemList;
    private MoodDatabase moodDatabase;

    public MoodViewModel(@NonNull Application application) {
        super(application);
        moodDatabase = MoodDatabase.getInstance(application);
        moodItemList = new MutableLiveData<>();
        loadMoodItems();
    }

    public MutableLiveData<List<MoodItem>> getMoodItemList() {
        return moodItemList;
    }

    public void loadMoodItems() {
        new Thread(() -> {
            List<MoodItem> items = moodDatabase.moodDao().getAllMoodItems();
            moodItemList.postValue(items);
        }).start();
    }

    public void insertMoodItem(MoodItem moodItem) {
        new Thread(() -> {
            moodDatabase.moodDao().insertMoodItem(moodItem);
            loadMoodItems();
        }).start();
    }

    public void deleteAll() {
        new Thread(() -> {
            moodDatabase.moodDao().deleteAll();
            loadMoodItems(); // 删除后重新加载数据
        }).start();

    }
}
